package com.wan.data.mysql.orm.convert;

import com.wan.data.mysql.exception.DBInitializeException;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.core.strategy.Strategy;

import java.util.Arrays;

/**
 * 默认转换器
 * 没有实际功能，代表会根据类型自动去获取转换器
 */
@Strategy(creator = AutoConverterCreator.class)
public interface AutoConverter extends Converter{

    static boolean isAuto(Class<? extends Converter> converter){
        return AutoConverter.class.isAssignableFrom(converter);
    }

    /**
     * 要转换的类型
     */
    Class<?>[] convertType();

    @Override
    default void tryCheck(ColumnMeta columnMeta) throws DBInitializeException {
        Class<?>[] classes = convertType();
        if(classes == null || classes.length == 0){
            return;
        }
        if(Arrays.stream(classes).noneMatch(cls->cls == columnMeta.getFieldType())){
            throw new DBInitializeException("字段不符合规则 -> " + columnMeta);
        }
    }
}
